package cn.hhhy.controller;

import cn.hhhy.pojo.*;
import cn.hhhy.service.UserService;
import cn.hhhy.service.VillageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/village")
public class VillageController {
    @Autowired
    private VillageService villageService;

    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return villageService.findPage(pageBean);
    }
    // 根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Village v = villageService.findById(id);
            return new Result(true,"查询成功",v);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    // 获取全部
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Village> villages = villageService.findAll();
            return new Result(true,"查询成功",villages);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    // 添加
    @RequestMapping("/add")
    public Result add(@RequestBody Village village) {
        try {
            villageService.add(village);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    // 删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            villageService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    // 修改
    @RequestMapping("/update")
    public Result update(@RequestBody Village village) {
        try {
            villageService.update(village);
            return new Result(true,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更新失败");
        }

    }

    // 根据用户id获取小区
    @RequestMapping("/findByUID")
    public Result findByUID(Integer id) {
        try {
            List<Village> villages = villageService.findByUID(id);
            return new Result(true, "获取成功",villages);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取失败");
        }
    }

}
